package com.qf.business.auth.service.impl;

import com.qf.business.auth.service.IAuthService;
import com.qf.business.user.feign.TeacherFeign;
import com.qf.commons.base.result.Codes;
import com.qf.commons.base.user.BaseUser;
import com.qf.commons.web.exception.ServiceException;
import com.qf.data.auth.vo.input.AuthLoginVo;
import com.qf.data.user.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * author Ken
 * create_time 2022/11/25
 */
@Service("authService0")
public class TeacherAuthServiceImpl implements IAuthService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private TeacherFeign teacherFeign;

    /**
     * 教师端的登录 基于邮箱的方式登录
     * @param loginVo
     */
    @Override
    public BaseUser login(AuthLoginVo loginVo) {
        //教师端的登录流程
        //1、验证码是否有效
        String serverCode = redisTemplate.opsForValue().get(loginVo.getEmail());
        if (serverCode == null || !serverCode.equals(loginVo.getCode())){
            //验证码校验未通过
            throw new ServiceException(Codes.CODE_ERROR);
        }

        //2、验证码通过 - 删除验证码
        redisTemplate.delete(loginVo.getEmail());

        //3、进行教师端的登录， 通过邮箱查询教师的信息
        Teacher teacher = teacherFeign.queryByEmail(loginVo.getEmail()).getData();
        return teacher;
    }
}
